package com.charles.event.listener;


import com.charles.config.ExportProperties;
import com.charles.context.ExportContext;
import com.charles.event.ExportEvent;
import com.charles.service.ExportRecordHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;

/**
 * @author charles
 * @date 2021/12/22 21:05
 */
@Slf4j
@Async(value = "exportThreadPool")
public class ExportEventListener implements ApplicationListener<ExportEvent> {

    @Autowired
    private ExportProperties exportProperties;

    @Autowired
    private ExportRecordHandler exportHandler;

    @Override
    public void onApplicationEvent(ExportEvent exportEvent) {
        ExportContext exportContext = ExportContext.builder().build();
        exportContext.getAttributes().put("exportEvent", exportEvent);
        try {
            //前置操作
            exportHandler.preHandle(exportContext);

            exportHandler.doHandle(exportContext);
            //更新记录
            exportHandler.postHandle(exportContext);
        } catch (Exception e) {
            e.printStackTrace();
            exportHandler.handleException(exportContext, e);
        }
    }


}
